import googlemap.GoogleStaticMap;
import googlemap.GoogleStaticMapFactory;
import googlemap.LatLng;
import googlemap.MapImageI;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 * Created by Connor Madden.
 * User: Connor Madden
 * Date: 1/04/12
 * Time: 8:24 PM
 */
public class MapDisplay {
    private JPanel panel1;
    private JTextField latTextField;
    private JTextField sizeXTextField;
    private JTextField sizeYTextField;
    private JTextField lngTextField;
    private JComboBox zoomComboBox;
    private JComboBox scaleComboBox;
    private JComboBox formatComboBox;
    private JComboBox mapTypeComboBox;
    private JButton applyButton;
    private JPanel mapPanel;
    private JLabel mapImageLabel;
    private JFrame jFrame;

    MapImageI mapImage;
    boolean wasDragged = false;
    Point pressLoc;

    public MapDisplay() {
        applyButton.addActionListener(new ActionListener() {

            /**
             * Invoked when an action occurs.
             */
            public void actionPerformed(ActionEvent e) {
                paint();
            }
        });
        mapImageLabel.addMouseListener(new MouseAdapter() {
            /**
             * {@inheritDoc}
             */
            @Override
            public void mouseClicked(MouseEvent e) {
                System.out.println(mapImage.fromPointToLatLng(e.getPoint()).toString());
            }

            /**
             * {@inheritDoc}
             */
            @Override
            public void mousePressed(MouseEvent e) {
                pressLoc = e.getPoint();
            }

            /**
             * {@inheritDoc}
             */
            @Override
            public void mouseReleased(MouseEvent e) {
               if (wasDragged){
                   Point releaseLoc = e.getPoint();
                   
                   int changeX = releaseLoc.x - pressLoc.x;
                   int changeY = releaseLoc.y - pressLoc.y;
                   
                   Point centre = mapImage.fromLatLngToPoint(mapImage.getCentreLatLng());
                   centre.translate(-changeX,changeY);

                   LatLng latLng = GoogleStaticMap.validateLatLng(mapImage.fromPointToLatLng(centre));

                   latTextField.setText(String.valueOf(latLng.getLat()));
                   lngTextField.setText(String.valueOf(latLng.getLng()));
               }
                wasDragged = false;
                paint();
            }
        });
        mapImageLabel.addMouseWheelListener(new MouseAdapter() {

            /**
             * {@inheritDoc}
             *
             * @since 1.6
             */
            @Override
            public void mouseWheelMoved(MouseWheelEvent e) {
                int i = zoomComboBox.getSelectedIndex() - e.getWheelRotation();
                if (i >= 0 && i <= zoomComboBox.getItemCount())
                    zoomComboBox.setSelectedIndex(i);
                paint();
            }
        });
        mapImageLabel.addMouseMotionListener(new MouseAdapter() {
            /**
             * {@inheritDoc}
             *
             * @since 1.6
             */
            @Override
            public void mouseDragged(MouseEvent e) {
                wasDragged = true;
            }
        });
    }

    private void paint() {
        GoogleStaticMapFactory.Format format;
        if (formatComboBox.getSelectedItem().toString().equals("PNG"))
            format = GoogleStaticMapFactory.Format.PNG;
        else if (formatComboBox.getSelectedItem().toString().equals("PNG32")) {
            format = GoogleStaticMapFactory.Format.PNG32;
        } else if (formatComboBox.getSelectedItem().toString().equals("GIF"))
            format = GoogleStaticMapFactory.Format.GIF;
        else if (formatComboBox.getSelectedItem().toString().equals("JPG"))
            format = GoogleStaticMapFactory.Format.JPG;
        else if (formatComboBox.getSelectedItem().toString().equals("JPG_BASELINE"))
            format = GoogleStaticMapFactory.Format.JPG_BASELINE;
        else
            format = GoogleStaticMapFactory.Format.PNG;

        GoogleStaticMapFactory.MapType mapType;
        if (mapTypeComboBox.getSelectedItem().toString().equals("ROAD_MAP"))
            mapType = GoogleStaticMapFactory.MapType.ROAD_MAP;
        else if (mapTypeComboBox.getSelectedItem().toString().equals("SATELLITE")) {
            mapType = GoogleStaticMapFactory.MapType.SATELLITE;
        } else if (mapTypeComboBox.getSelectedItem().toString().equals("TERRAIN"))
            mapType = GoogleStaticMapFactory.MapType.TERRAIN;
        else if (mapTypeComboBox.getSelectedItem().toString().equals("HYBRID"))
            mapType = GoogleStaticMapFactory.MapType.HYBRID;
        else
            mapType = GoogleStaticMapFactory.MapType.ROAD_MAP;

        ImageIcon imageIcon;
        mapImage = GoogleStaticMapFactory.newGoogleStaticMap(
                new LatLng(Float.valueOf(latTextField.getText()), Float.valueOf(lngTextField.getText())),
                Integer.valueOf((String) zoomComboBox.getSelectedItem()),
                Integer.valueOf(sizeXTextField.getText()),
                Integer.valueOf(sizeYTextField.getText()),
                Integer.valueOf((String) scaleComboBox.getSelectedItem()),
                format,
                mapType);
        imageIcon = new ImageIcon(mapImage.getImage());
        mapImageLabel.setIcon(imageIcon);
        jFrame.pack();
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("MapDisplay");
        MapDisplay mapDisplay = new MapDisplay();
        mapDisplay.jFrame = frame;
        frame.setContentPane(mapDisplay.panel1);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mapDisplay.paint();
        frame.setVisible(true);
    }

    private void createUIComponents() {
        mapPanel = new JPanel();
    }

}
